import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.util.WindowUtil;
import org.rsbot.util.GlobalConfiguration.Paths;

final class VoluntaryThieve$Update
{
  private final String URL_VERSION_CHECK = "http://vilon.site90.net/scripts/" + VoluntaryThieve.class.getName().toLowerCase() + "/.version";

  private final String URL_LATEST_VERSION = "http://vilon.site90.net/scripts/" + VoluntaryThieve.class.getName().toLowerCase() + "/.latest";

  private VoluntaryThieve$Update(VoluntaryThieve paramVoluntaryThieve)
  {
  }

  private double getVersion()
  {
    double d = -1.0D;
    BufferedReader localBufferedReader = null;
    try
    {
      URLConnection localURLConnection = new URL(this.URL_VERSION_CHECK).openConnection();
      localBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream()));
      d = Double.parseDouble(localBufferedReader.readLine());
    } catch (Exception localIOException2) {
    } finally {
      if (localBufferedReader != null)
        try {
          localBufferedReader.close();
        }
        catch (IOException localIOException3)
        {
        }
    }
    return d;
  }
  private boolean downloadUpdate() { String str = GlobalConfiguration.Paths.getScriptsSourcesDirectory() + File.separator + VoluntaryThieve.class.getName();

    BufferedReader localBufferedReader = null;
    BufferedWriter localBufferedWriter = null;
    boolean bool = false;
    Object localObject1;
    try { URLConnection localURLConnection = new URL(this.URL_LATEST_VERSION).openConnection();
      localBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream()));
      localBufferedWriter = new BufferedWriter(new FileWriter(str + ".update"));

      while ((localObject1 = localBufferedReader.readLine()) != null) {
        localBufferedWriter.write((String)localObject1);
        localBufferedWriter.newLine();
      }

      bool = true;
    }
    catch (IOException localFile2)
    {
    }
    catch (Exception localException)
    {
      File localFile1;
      File localFile2;
      throw new AssertionError(localException.getMessage());
    } finally {
      if (localBufferedReader != null)
        try {
          localBufferedReader.close();
        }
        catch (IOException localIOException8)
        {
        }
      if (localBufferedWriter != null) {
        try {
          localBufferedWriter.flush();
        }
        catch (IOException localIOException9) {
        }
        try {
          localBufferedWriter.close();
        }
        catch (IOException localIOException10) {
        }
      }
      if (!bool) {
        File localFile4 = new File(str + ".update");
        if ((localFile4.exists()) && (
          (!localFile4.canWrite()) || (!localFile4.delete()))) {
          VoluntaryThieve.access$13900(this.this$0).warning("Unable to delete: " + str + ".update");
        }

        VoluntaryThieve.access$14000(this.this$0).warning("Failed when downloading the latest version.");
      }
    }
    File localFile3;
    if (bool) {
      localFile3 = new File(str + ".java");
      if (localFile3.exists()) {
        if ((bool = (localFile3.canWrite()) && (localFile3.delete()) ? 1 : 0) == 0) {
          VoluntaryThieve.access$14100(this.this$0).warning("Unable to delete: " + localFile3.getPath());
        }
      }
      if (bool) {
        localObject1 = new File(str + ".update");
        if ((bool = ((File)localObject1).renameTo(new File(str + ".java")))) {
          this.this$0.log("The latest version has been downloaded successfully.");
          this.this$0.log("Please recompile your scripts and restart the script."); } else {
          VoluntaryThieve.access$14200(this.this$0).warning("Unable to rename: " + ((File)localObject1).getPath());
        }
      }
    }
    if (!bool) {
      localFile3 = new File(str + ".update");
      if ((localFile3.exists()) && (
        (!localFile3.canWrite()) || (!localFile3.delete()))) {
        VoluntaryThieve.access$14300(this.this$0).warning("Unable to delete: " + str + ".update");
      }
    }

    return bool;
  }

  private boolean check()
  {
    String str = VoluntaryThieve.Configuration.access$11900(VoluntaryThieve.access$11800(this.this$0), "AllowUpdates", null);
    if (str == null) {
      int i = WindowUtil.showConfirmDialog("Do you give your permission to let the script check for updates?", 1);

      if (i == 0) {
        VoluntaryThieve.Configuration.access$12000(VoluntaryThieve.access$11800(this.this$0), "AllowUpdates", String.valueOf(true));
        str = String.valueOf(true);
      } else if (i == 1) {
        VoluntaryThieve.Configuration.access$12000(VoluntaryThieve.access$11800(this.this$0), "AllowUpdates", String.valueOf(false));
      }
    }
    if (!Boolean.parseBoolean(str)) {
      return false;
    }
    this.this$0.log("Checking for available updates...");
    double d = getVersion();
    if (d == -1.0D) {
      VoluntaryThieve.access$14400(this.this$0).warning("Unable to retrieve information about latest version.");
    } else if (d <= VoluntaryThieve.access$11000().version()) {
      this.this$0.log("Script is fully up to date (version " + VoluntaryThieve.access$11000().version() + ").");
    } else {
      int j = WindowUtil.showConfirmDialog("A new version (v" + d + ") is available.\n" + "Would you like to update now?", 1);

      if (j == 0) {
        return downloadUpdate();
      }
    }
    return false;
  }
}